#include "widget.h"
#include "ui_widget.h"

#include <QCloseEvent>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QWheelEvent>
#include "mybutton.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(oncurrentIndexChanged(int)));
    //    connect(this, SIGNAL(mysignal()), this, SLOT(myslot()));
    //    connect(this, SIGNAL(mysignalparams(int)), this, SLOT(myslotparams(int)));
    //    emit mysignal();
    //    emit mysignalparams(100);
    connect(ui->mybtn, &MyButton::clicked, [=](){
        qDebug() << "myButton is clicked";
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::enterEvent(QEvent *event)
{
    qDebug() << "mouse enter";
}

void Widget::leaveEvent(QEvent *event)
{
    qDebug() << "mouse leave";
}

void Widget::wheelEvent(QWheelEvent *event)
{

    qDebug() << event->angleDelta();
}

void Widget::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::warning(this, tr("My Application"),
                                   tr("关闭窗口\n"
                                      "你想关闭窗口吗?"),
                                   QMessageBox::Ok | QMessageBox::No
                                   );

    switch (ret) {
    case QMessageBox::Ok :
         event->accept();
        break;
    case QMessageBox::No :
         event->ignore();
        break;
    }
}

void Widget::resizeEvent(QResizeEvent *event)
{
    qDebug() << "oldSize:" << event->oldSize()
             << "newSize:" << event->size();
}

//void Widget::myslot()
//{
//    std::cout << "myslot" << std::endl;
//}

//void Widget::myslotparams(int value)
//{
//    qDebug() << "myslotparams";
//    qDebug() << value;
//}


void Widget::on_btnRead_clicked()
{
    //    QFile file("E:/QT2/test.txt");
    QFile file;
    file.setFileName("E:/QT/test.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "file open error";
    }
    int size = file.size();
    char* context = new char(size);
    char* con = (char *)malloc(sizeof(char)*size);
    //    char context[100] = {"\0"};
    if(file.read(context, 100) == -1) return;
    qDebug() << context;
    file.close();
}


void Widget::on_btnWrite_clicked()
{
    QFile file("E:/QT/test2.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    file.write("Program 45-QFile001 write something to This File 我是测试");
}


void Widget::on_btnstreamRead_clicked()
{
    QFile file;
    file.setFileName("E:/QT/test.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "file open error";
    }

    QTextStream in(&file);
    in.setCodec("UTF-8");
    //QString context = in.read(file.size());
    while(!in.atEnd()){
        QString context = in.readLine();
        qDebug() << context;
        qDebug() << "---------";
    }

    file.close();
}


void Widget::on_btnstreamWrite_clicked()
{
    QFile file;
    file.setFileName("E:/QT/test3.txt");
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        qDebug() << "file open error";
    }
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << "I write stream char to file ";
    file.close();
}


void Widget::on_btnQFileDialog_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "E:/QT/",
                                                    tr(" Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"));
    qDebug() << fileName;

    QFile file;
    file.setFileName(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "file open error";
    }

    QTextStream in(&file);
    in.setCodec("UTF-8");
    //QString context = in.read(file.size());
    while(!in.atEnd()){
        QString context = in.readLine();
        qDebug() << context;
        qDebug() << "---------";
    }

    file.close();
}


void Widget::on_btnSe_clicked()
{
    //    int arr[120];
    //    for (int i : arr) {

    //    }

    QFileDialog qFileDialog;
    qFileDialog.setFileMode(QFileDialog::ExistingFiles);
    qFileDialog.setNameFilter("*.txt");
    qFileDialog.exec();
    QStringList qstrings = qFileDialog.selectedFiles();
    for(QString str : qstrings){
        qDebug() << str;
    }
}


void Widget::on_btnSave_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                                    "E:/QT/untitled.txt",
                                                    tr("Text (*.txt *.doc)"));
    qDebug() << fileName;
    QFile file;
    file.setFileName(fileName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        qDebug() << "file open error";
    }
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << "Qdialog Write Data to the Txt File";
    file.close();
}

void Widget::oncurrentIndexChanged(int index)
{
    qDebug() << index;
    qDebug() << ui->comboBox->currentText();
}

